import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import '../utils/themes/app_typography.dart';

/// 文本样式服务
/// 提供在GetX框架中访问和管理文本样式的便捷方法
class TextStyleService extends GetxService {
  // 存储实例
  final GetStorage _storage = GetStorage();
  
  // 当前文本缩放因子
  final RxDouble _textScaleFactor = 1.0.obs;
  double get textScaleFactor => _textScaleFactor.value;
  
  // 字体大小偏好
  final RxString _fontSizePreference = 'normal'.obs;
  String get fontSizePreference => _fontSizePreference.value;
  
  /// 初始化服务
  Future<TextStyleService> init() async {
    debugPrint('TextStyleService 初始化');
    
    // 从存储中加载字体大小偏好
    _fontSizePreference.value = _storage.read('fontSizePreference') ?? 'normal';
    
    // 根据偏好设置文本缩放因子
    _updateTextScaleFactor();
    
    return this;
  }
  
  /// 更新文本缩放因子
  void _updateTextScaleFactor() {
    switch (_fontSizePreference.value) {
      case 'small':
        _textScaleFactor.value = 0.85;
        break;
      case 'normal':
        _textScaleFactor.value = 1.0;
        break;
      case 'large':
        _textScaleFactor.value = 1.15;
        break;
      case 'extra_large':
        _textScaleFactor.value = 1.3;
        break;
      default:
        _textScaleFactor.value = 1.0;
    }
  }
  
  /// 设置字体大小偏好
  void setFontSizePreference(String preference) {
    if (_fontSizePreference.value == preference) return;
    
    _fontSizePreference.value = preference;
    _updateTextScaleFactor();
    
    // 保存到存储
    _storage.write('fontSizePreference', preference);
    
    // 通知应用更新
    Get.forceAppUpdate();
  }
  
  /// 缩放文本样式
  TextStyle scaleTextStyle(TextStyle style) {
    return style.copyWith(
      fontSize: style.fontSize! * _textScaleFactor.value,
    );
  }
  
  /// 获取标题大样式
  TextStyle headlineLarge({Color? color}) {
    final style = AppTypography.headline.large;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标题中样式
  TextStyle headlineMedium({Color? color}) {
    final style = AppTypography.headline.medium;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标题小样式
  TextStyle headlineSmall({Color? color}) {
    final style = AppTypography.headline.small;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标题1样式
  TextStyle titleLarge({Color? color}) {
    final style = AppTypography.headline.titleLarge;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标题2样式
  TextStyle titleMedium({Color? color}) {
    final style = AppTypography.headline.titleMedium;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标题3样式
  TextStyle titleSmall({Color? color}) {
    final style = AppTypography.headline.titleSmall;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取正文大样式
  TextStyle bodyLarge({Color? color}) {
    final style = AppTypography.body.large;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取正文中样式
  TextStyle bodyMedium({Color? color}) {
    final style = AppTypography.body.medium;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取正文小样式
  TextStyle bodySmall({Color? color}) {
    final style = AppTypography.body.small;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标签大样式
  TextStyle labelLarge({Color? color}) {
    final style = AppTypography.label.large;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标签中样式
  TextStyle labelMedium({Color? color}) {
    final style = AppTypography.label.medium;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取标签小样式
  TextStyle labelSmall({Color? color}) {
    final style = AppTypography.label.small;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取状态标签样式
  TextStyle statusLabel({Color? color}) {
    final style = AppTypography.label.status;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取计数标签样式
  TextStyle countLabel({Color? color}) {
    final style = AppTypography.label.count;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取主按钮样式
  TextStyle buttonPrimary({Color? color}) {
    final style = AppTypography.button.primary;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
  
  /// 获取小按钮样式
  TextStyle buttonSmall({Color? color}) {
    final style = AppTypography.button.small;
    return color != null 
        ? scaleTextStyle(style).copyWith(color: color)
        : scaleTextStyle(style);
  }
} 